package com.xjgz.aio.socket;

import com.xjgz.aio.socket.pro.timer.HashedWheelTimer;
import com.xjgz.aio.socket.pro.timer.Timer;

import java.time.LocalDateTime;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

public class TimerTask {

    public static void main(String[] args) {
        Timer timer = new HashedWheelTimer(Thread::new);
        timer.scheduleWithFixedDelay(() -> System.out.println(LocalDateTime.now()), 1, TimeUnit.SECONDS);
        System.out.println("success");
    }

    public static void main1(String[] args) throws InterruptedException {
        Timer timer = new HashedWheelTimer(Thread::new);
        int i = 100000;
        AtomicInteger count = new AtomicInteger();
        CountDownLatch countDownLatch = new CountDownLatch(i);
        while (i-- > 0) {
            int delay = ((int) (Math.random() * 1000)) % 10 + ((int) (Math.random() * 1000)) % 10;
            long endTime = System.currentTimeMillis() + delay;
            timer.schedule(() -> {
                long delay1 = System.currentTimeMillis() - endTime;
                System.out.println(endTime + " -> " + count.incrementAndGet() + ": " + delay1);
                countDownLatch.countDown();
            }, delay, TimeUnit.MILLISECONDS);
        }
        // ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
        // executorService.scheduleWithFixedDelay(() -> System.out.println(countDownLatch.getCount()), 1, 1, TimeUnit.SECONDS);
        countDownLatch.await();
        System.out.println("success");
        timer.shutdown();
        // executorService.shutdown();
    }
}
